package com.tz.tzgw.pc.web.dao;

import com.tz.tzgw.pc.web.model.Banner;
import com.tz.tzgw.pc.web.model.PageResult;
import com.tz.tzgw.pc.web.model.ZtArticle;
import com.tz.tzgw.pc.web.model.ZtSet;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;

public class ZtSetDao {
    private final JdbcTemplate jdbcTemplate;

    public ZtSetDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


    public PageResult<ZtSet> getZtSetByPageAndCondition(int pageNum, int pageSize, String name, Integer isshow) {
        PageResult<ZtSet> pageResult = new PageResult<>();
        int offset = (pageNum - 1) * pageSize;

        // 构建查询总条数的 SQL
        StringBuilder countSqlBuilder = new StringBuilder("SELECT COUNT(*) FROM tz_zt_set");
        List<Object> countParams = new ArrayList<>();

        // 构建分页查询的 SQL
        StringBuilder querySqlBuilder = new StringBuilder("SELECT * FROM tz_zt_set");
        List<Object> queryParams = new ArrayList<>();

        // 判断 title 是否为空，并构建相应的查询条件
        if (name != null && !name.isEmpty()) {
            countSqlBuilder.append(" WHERE name LIKE ?");
            countParams.add("%" + name + "%");

            querySqlBuilder.append(" WHERE name LIKE ?");
            queryParams.add("%" + name + "%");
        }


        String wherestr = " WHERE";

        if (name != null){
            countSqlBuilder.append(" WHERE name LIKE ?");
            countParams.add("%" + name + "%");

            querySqlBuilder.append(" WHERE name LIKE ?");
            queryParams.add("%" + name + "%");
        }

        if (isshow != null){
            if (name != null){
                wherestr = " AND";
            }
            countSqlBuilder.append(wherestr+" isshow = ?");
            querySqlBuilder.append(wherestr+" isshow = ?");
            countParams.add(isshow);
            queryParams.add(isshow);
        }

        // 查询总条数
        String countSql = countSqlBuilder.toString();
        int total = jdbcTemplate.queryForObject(countSql, Integer.class, countParams.toArray());
        pageResult.setTotal(total);

        // 分页查询
        querySqlBuilder.append(" ORDER BY sort_order DESC LIMIT ?, ?");
        queryParams.add(offset);
        queryParams.add(pageSize);

        String querySql = querySqlBuilder.toString();
        List<ZtSet> sets = jdbcTemplate.query(querySql, new BeanPropertyRowMapper<>(ZtSet.class), queryParams.toArray());
        pageResult.setData(sets);

        return pageResult;
    }


    public int insertOrUpdateZtSet(ZtSet ztSet) {
        if (ztSet.getId() != null) {
            String sql = "UPDATE tz_zt_set SET name = ?, thumb = ?, banner = ?, isshow = ?, sort_order = ?, status= ? WHERE id = ?";
            jdbcTemplate.update(sql, ztSet.getName(), ztSet.getThumb(),ztSet.getBanner(), ztSet.getIsshow(), ztSet.getSort_order(), ztSet.getStatus(), ztSet.getId());
        } else {
            String sql = "INSERT INTO tz_zt_set (name, thumb, banner, isshow, status , sort_order) VALUES (?, ?, ?, ?, ?, ?)";
            jdbcTemplate.update(sql, ztSet.getName(), ztSet.getThumb(),ztSet.getBanner(), ztSet.getIsshow(),ztSet.getStatus(), ztSet.getSort_order());
        }
        return  1;
    }


    public List<ZtSet> getAllZtSets(Integer isshow) {
        StringBuilder sqlBuilder = new StringBuilder("SELECT * FROM tz_zt_set WHERE status=1 ");


        List<Object> queryParams = new ArrayList<>();
        if (isshow != null) {
            sqlBuilder.append(" AND isshow = ?");

            queryParams.add(isshow);
        }

        sqlBuilder.append(" ORDER BY sort_order DESC");
        String sql = sqlBuilder.toString();

        List<ZtSet> result = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ZtSet.class), queryParams.toArray());
        return result != null ? result : new ArrayList<ZtSet>();
    }

    public Integer deleteZtSet(Integer id) {
        String sql = "DELETE FROM tz_zt_set WHERE id = ?";
        jdbcTemplate.update(sql, id);
        return 1;
    }

    public ZtSet getZtSetById(Long id) {
        String sql = "SELECT * FROM tz_zt_set WHERE id = ?";
        return jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<>(ZtSet.class));
    }
}
